package br.fatec.ads.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import br.fatec.ads.dao.RespostaDAO;
import br.fatec.ads.exception.DataBaseException;
import br.fatec.ads.exception.RespostaException;
import br.fatec.ads.model.Resposta;

public class RespostaAlterar extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public RespostaAlterar() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			HttpSession session = request.getSession(false);
			
			Resposta resposta = new Resposta();
			RespostaDAO respostaDAO = new RespostaDAO();
		
			resposta = respostaDAO.getResposta(Integer.parseInt(request.getParameter("idRespostaAlterar")));
			
			session.setAttribute("resposta", resposta);
			 
			request.getRequestDispatcher("cadresposta_a.jsp").forward(request, response);
		} catch (RespostaException e) {
			e.printStackTrace();
		} catch (DataBaseException e) {
			e.printStackTrace();
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			HttpSession session = request.getSession(false);
			Resposta resposta = new Resposta();
			
			resposta.setIdResposta(Integer.parseInt(request.getParameter("idRespostaAlterar")));
			resposta.setNroResposta(request.getParameter("txtNroResposta"));
			resposta.setUsuario(session.getAttribute("usuarioDesc").toString());
			
			RespostaDAO respostaDAO = new RespostaDAO();
			
			respostaDAO.alterarResposta(resposta);
			
			resposta = respostaDAO.getResposta(resposta.getIdResposta());
			
			request.getRequestDispatcher("RespostaPesquisar?acao=SUCESSO_ALTERAR&idPergunta=" + resposta.getPergunta().getIdPergunta()).forward(request, response);
		} catch (RespostaException e) {
			request.getRequestDispatcher("cadresposta_e.jsp?acao=erro").forward(request, response);
			e.printStackTrace();
		} catch (DataBaseException e) {
			request.getRequestDispatcher("cadresposta_e.jsp?acao=erro").forward(request, response);
			e.printStackTrace();
		}
	}
}